package com.osfw.framework.config;

import com.osfw.framework.utils.YamlUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

@EnableAsync
@Configuration
@ConfigurationProperties(prefix = "osfw")
public class Global {

    private static final Logger log = LoggerFactory.getLogger(Global.class);

    /**
     * 项目名称
     */
    private static String name;

    /**
     * 版本
     */
    private static String version;

    /**
     * 版权年份
     */
    private static String copyrightYear;

    /**
     * 获取地址开关
     */
    private static boolean addressEnabled;

    /**
     * 基础身份校验开关
     */
    private static boolean saBasicEnabled;

    /**
     * 临时授权校验开关
     */
    private static boolean saTempEnabled;

    /**
     * 二次身份认证开关
     */
    private static boolean saSafeEnabled;

    /**
     * 登录验证码开关
     */
    private static boolean captchaEnabled;

    /**
     * 登录验证码类型
     */
    private static String captchaType;

    /**
     * 网关地址
     */
    private static String gatewayUrl;

    /**
     * 登录地址
     */
    private static String loginUrl;

    /**
     * 首页地址
     */
    private static String indexUrl;




    /**
     * 保存全局属性值
     */
    private static Map<String, String> map = new HashMap<String, String>();

    private static String NAME = "config/application.yml";

    /**
     * 获取配置（获取途径-配置文件）
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (StringUtils.isEmpty(value)) {
            Map<?, ?> yamlMap = null;
            try {
                yamlMap = YamlUtils.loadYaml(NAME);
                value = String.valueOf(YamlUtils.getProperty(yamlMap, key));
                map.put(key, value != null ? value : StringUtils.EMPTY);
            } catch (FileNotFoundException e) {
                log.error("获取全局配置异常 {}", key);
            }
        }
        return value == null || value.equals("null") ? "" : value;
    }


    public static String getName() {
        return name;
    }

    public void setName(String name) {
        Global.name = name;
    }

    public static String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        Global.version = version;
    }

    public static String getCopyrightYear() {
        return copyrightYear;
    }

    public void setCopyrightYear(String copyrightYear) {
        Global.copyrightYear = copyrightYear;
    }


    public static boolean isAddressEnabled() {
        return addressEnabled;
    }

    public void setAddressEnabled(boolean addressEnabled) {
        Global.addressEnabled = addressEnabled;
    }

    public static boolean isSaBasicEnabled() {
        return saBasicEnabled;
    }

    public void setSaBasicEnabled(boolean saBasicEnabled) {
        Global.saBasicEnabled = saBasicEnabled;
    }

    public static boolean isSaTempEnabled() {
        return saTempEnabled;
    }

    public void setSaTempEnabled(boolean saTempEnabled) {
        Global.saTempEnabled = saTempEnabled;
    }

    public static boolean isSaSafeEnabled() {
        return saSafeEnabled;
    }

    public void setSaSafeEnabled(boolean saSafeEnabled) {
        Global.saSafeEnabled = saSafeEnabled;
    }

    public static boolean isCaptchaEnabled() {
        return captchaEnabled;
    }

    public void setCaptchaEnabled(boolean captchaEnabled) {
        Global.captchaEnabled = captchaEnabled;
    }

    public static String getCaptchaType() {
        return captchaType;
    }

    public void setCaptchaType(String captchaType) {
        Global.captchaType = captchaType;
    }

    public static String getGatewayUrl() {
        return gatewayUrl;
    }

    public void setGatewayUrl(String gatewayUrl) {
        Global.gatewayUrl = gatewayUrl;
    }

    public static String getLoginUrl() {
        return loginUrl;
    }

    public void setLoginUrl(String loginUrl) {
        Global.loginUrl = loginUrl;
    }

    public static String getIndexUrl() {
        return indexUrl;
    }

    public void setIndexUrl(String indexUrl) {
        Global.indexUrl = indexUrl;
    }
}
